package com.easyexcel.usr.controller;

import com.alibaba.excel.EasyExcel;
import com.easyexcel.usr.bean.Person;
import com.easyexcel.usr.exception.ExcelReadException;
import com.easyexcel.usr.listener.SimpleReadListener;
import com.easyexcel.usr.service.ReadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Package: com.easyexcel.usr.controller
 * @ClassName: ExcelReadController
 * @Author: shuaijun Wang
 * @CreateTime: 2020/11/4 15:44
 * @Description:
 */
@RestController
@RequestMapping("importPersonExcel")
@Slf4j
public class ExcelReadController {
    @Resource
    ReadService readService;

    @PostMapping("import")
    public void readSimpleExcel(MultipartFile file, HttpServletResponse response){
        try {
            EasyExcel.read(file.getInputStream(), Person.class, new SimpleReadListener(response,readService))
                    .sheet()
                    .doRead();
        } catch (ExcelReadException | IOException e) {
            log.error("错误代码：{},错误信息：{}",5001,e.getMessage());
        }

    }


}
